package com.ems.controller.supers.action;

import com.ems.pojo.data.ResultData;
import com.ems.pojo.data.RetunResult;
import com.ems.pojo.permission.Action;
import com.ems.pojo.user.User;
import com.ems.service.supers.action.ActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 伟伟
 * @Date: 2019/10/5
 * @Description: 权限
 * @Version: 1.0
 */
@Controller
@CrossOrigin //允许所有请求跨域
@RequestMapping("/actionController")
public class actionController {
    @Autowired
    @Qualifier("actionService")
    private ActionService actionService;

    /**
     * 获取所有权限信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAllAction")
    public ResultData getAllAction(){
        List<Action> list = actionService.getAllAction();
        return RetunResult.AssemblyData(list);
    }

    /**
     * 修改指定的权限
     */
    @ResponseBody
    @RequestMapping("/updateAction")
    public ResultData updateAction(@RequestBody Action action){
        if(actionService.updateAction(action) > 0){
            return RetunResult.AssemblyData();
        }
        return RetunResult.AssemblyData("failure.to.update.action");
    }

    /**
     * 用户登录后获取可操作的权限
     * @return
     */
    @RequestMapping("/getPermission")
    @ResponseBody
    public ResultData getPermission(@RequestBody User user){
        List<Action> list = actionService.getPermission(user.getId());
        return RetunResult.AssemblyData(list);
    }

    /**
     * 根据action_id查询指定的权限信息
     * @param action
     * @return
     */
    @ResponseBody
    @RequestMapping("/getActionByActionId")
    public ResultData getActionByActionId(@RequestBody Action action){
        return RetunResult.AssemblyData(actionService.getActionByActionId(action));
    }

    /**
     * 根据id删除对应的权限
     * @param action
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteActionById")
    public ResultData deleteActionById(@RequestBody Action action){
        actionService.deleteActionById(action);
        return RetunResult.AssemblyData();
    }
}
